import Vue from 'vue';
import { storage } from "src/util/common";
export const errorCode = {
  ELEMENT_TYPE_ERROR: 100,
  RANGE_ERROR: 110,
  NAME_RANGE_ERROR: 111,
  CONSTRAINT_ERROR: 120,
  EMPTY_ERROR: 130,
  CALCULATION_ERROR: 150,

  SYSTEM_ERROR: 4000,
  MODEL_CREATE_ERROR: 4001,
  MODEL_UPDATE_ERROR: 4002,
  MODEL_DI_CREATE_ERROR: 4003,
  MODEL_DI_UPDATE_ERROR: 4004,
  MODEL_CONSTRAINT_ERROR: 4005,
  CONTAINMENT_EXIST: 4006,
  CONTAINMENT_ERROR: 4007,
  OWNER_CANNOT_BE_YOURSELF: 4008,
  CHILD_CAN_NOT_BE_PARENT: 4009,
  CURRENT_PROJECT_NOT_EXISTS: 4010,
  PASTE_NOT_ALLOW: 4011,
	
  FILE_READ_ERROR: 5001,
  FILE_WRITE_ERROR: 5002,
  SAVE_PROJECT_ERROR: 5003,
  LOAD_PROJECT_ERROR: 5004,
  DEFAULT_ERROR: 5005,
  SER_ERROR: 5006,
  DESER_ERROR: 5007,

};

/*
StatusCodeEnum$SUCCESS_instance = new StatusCodeEnum('SUCCESS', 0, 200, '正常');
StatusCodeEnum$WARNINGS_instance = new StatusCodeEnum('WARNINGS', 1, 300, '警告');
StatusCodeEnum$ERROR_instance = new StatusCodeEnum('ERROR', 2, 400, '错误');
StatusCodeEnum$ELEMENT_TYPE_ERROR_instance = new StatusCodeEnum('ELEMENT_TYPE_ERROR', 3, 100, '对象类型不匹配');
StatusCodeEnum$RANGE_ERROR_instance = new StatusCodeEnum('RANGE_ERROR', 4, 110, '超出系统允许范围');
StatusCodeEnum$NAME_RANGE_ERROR_instance = new StatusCodeEnum('NAME_RANGE_ERROR', 5, 111, '名称输入超过规定长度，最长不能超过50个字符');
StatusCodeEnum$CONSTRAINT_ERROR_instance = new StatusCodeEnum('CONSTRAINT_ERROR', 6, 120, '超出模型约束范围');
StatusCodeEnum$EMPTY_ERROR_instance = new StatusCodeEnum('EMPTY_ERROR', 7, 130, '不允许为空');
StatusCodeEnum$CALCULATION_ERROR_instance = new StatusCodeEnum('CALCULATION_ERROR', 8, 150, '解析错误');
StatusCodeEnum$SYSTEM_ERROR_instance = new StatusCodeEnum('SYSTEM_ERROR', 9, 4000, '系统错误');
StatusCodeEnum$MODEL_CREATE_ERROR_instance = new StatusCodeEnum('MODEL_CREATE_ERROR', 10, 4001, '模型创建失败');
StatusCodeEnum$MODEL_UPDATE_ERROR_instance = new StatusCodeEnum('MODEL_UPDATE_ERROR', 11, 4002, '模型更新失败');
StatusCodeEnum$MODEL_DI_CREATE_ERROR_instance = new StatusCodeEnum('MODEL_DI_CREATE_ERROR', 12, 4003, 'DI创建失败');
StatusCodeEnum$MODEL_DI_UPDATE_ERROR_instance = new StatusCodeEnum('MODEL_DI_UPDATE_ERROR', 13, 4004, 'DI更新失败');
StatusCodeEnum$MODEL_CONSTRAINT_ERROR_instance = new StatusCodeEnum('MODEL_CONSTRAINT_ERROR', 14, 4005, '操作不符合规范');
StatusCodeEnum$CONTAINMENT_EXIST_instance = new StatusCodeEnum('CONTAINMENT_EXIST', 15, 4006, '已存在包含');
StatusCodeEnum$CONTAINMENT_ERROR_instance = new StatusCodeEnum('CONTAINMENT_ERROR', 16, 4007, '不可相互包含');
StatusCodeEnum$OWNER_CANNOT_BE_YOURSELF_instance = new StatusCodeEnum('OWNER_CANNOT_BE_YOURSELF', 17, 4008, '元素的所有者不能是自己');
StatusCodeEnum$CHILD_CAN_NOT_BE_PARENT_instance = new StatusCodeEnum('CHILD_CAN_NOT_BE_PARENT', 18, 4009, '子元素不能为父元素的父元素');
StatusCodeEnum$CURRENT_PROJECT_INSTANCE_POOL_NOT_EXISTS_instance = new StatusCodeEnum('CURRENT_PROJECT_INSTANCE_POOL_NOT_EXISTS', 19, 4010, '当前项目示例池不存在');
StatusCodeEnum$CURRENT_PROJECT_MODEL_CONFIG_POOL_NOT_EXISTS_instance = new StatusCodeEnum('CURRENT_PROJECT_MODEL_CONFIG_POOL_NOT_EXISTS', 20, 4012, '当前项目类型配置池不存在');
StatusCodeEnum$OWNER_INCONSISTENT_ERROR_instance = new StatusCodeEnum('OWNER_INCONSISTENT_ERROR', 21, 4013, '子元素的父级不一致');
StatusCodeEnum$NOT_SUPPORT_OPERATION_instance = new StatusCodeEnum('NOT_SUPPORT_OPERATION', 22, 4011, '不支持的操作');
StatusCodeEnum$ROOT_MODEL_CREATE_ERROR_instance = new StatusCodeEnum('ROOT_MODEL_CREATE_ERROR', 23, 4012, '根模型创建异常');
StatusCodeEnum$NAME_DUPLICATED_instance = new StatusCodeEnum('NAME_DUPLICATED', 24, 9001, '名称重复');
StatusCodeEnum$DEPENDENCY_ERROR_instance = new StatusCodeEnum('DEPENDENCY_ERROR', 25, 4014, '拓展包依赖的元素,必须放置在包下');
StatusCodeEnum$PROJECT_DATA_ERROR_instance = new StatusCodeEnum('PROJECT_DATA_ERROR', 26, 4015, '项目数据异常');
StatusCodeEnum$DD_CONFIG_ERROR_instance = new StatusCodeEnum('DD_CONFIG_ERROR', 27, 4016, 'DD配置异常');
StatusCodeEnum$PROPERTY_READONLY_CANNOT_MODIFY_ERROR_instance = new StatusCodeEnum('PROPERTY_READONLY_CANNOT_MODIFY_ERROR', 28, 4017, '只读属性不能修改');
StatusCodeEnum$IMPORT_PROFILE_ERROR_instance = new StatusCodeEnum('IMPORT_PROFILE_ERROR', 29, 4018, ' 导入失败，请勿重复导入');
StatusCodeEnum$IMPORT_PROFILE_ELEMENT_ERROR_instance = new StatusCodeEnum('IMPORT_PROFILE_ELEMENT_ERROR', 30, 4019, '导入失败 扩展包内的元素已存在，请勿重复导入');
*/

export const errorConfig = {
  100: '对象类型不匹配',
  110: '超出系统允许范围',
  111: '名称输入超过规定长度，最长不能超过50个字符',
  120: '超出模型约束范围',
  130: '不允许为空',
  150: '解析错误',
  400: '错误',

  4000: '系统错误',
  4001: '模型创建失败',
  4002: '模型更新失败',
  4003: 'DI创建失败',
  4004: 'DI更新失败',
  4005: '操作不符合规范',
  4006: '已存在包含',
  4007: '不可相互包含',
  4008: '元素的所有者不能是自己',
  4009: '子元素不能为父元素的父元素',
  4010: '当前项目不存在',
  4011: '粘帖失败，复制元素不允许挂载在当前节点下',
  4015: '项目数据异常',

  5001: '读取文件失败',
  5002: "写入文件失败",
  5003: '保存项目失败',
  5004: '加载项目失败',

  5005: '操作失败',
  5006: '序列化失败',
  5007: '反序列化失败',
  5008: '文件删除失败',

  5009: '删除失败',

  6001: '服务器上已存在相同名称的项目，请修改项目名称',
  6002: '提交失败',
  6003: '没有可提交的更改',
  6011: '更新失败，请稍候再试。',

};

const fsErrorCode = {
  ENOENT: '文件不存在',
  EPERM: '缺少文件系统权限'
};

//// 包装一个error对象，增加info属性，info中存了code
export const createError = (status, error) => {
  if (!error){
    error = new Error();
  }
  if (error.info) return error;
  error.info = { code: status };

  return error;

};

export const dumpLogError = (...args) => {
  //// 按照日期保存错误日志
  try {
    let dump = window.$ele.util.inspect(args, { compact: false, depth: 6, breakLength: 80 });
    console.log(...args);
    let fileName = new Date();
    let logName = fileName.toLocaleString();
    fileName = fileName.toLocaleDateString().replace(/\////g, '-') + '.log';
    //// 这个路径会有mac兼容问题 
    let appData = window.$ele.electron.remote.app.getPath('userData') + '\\';
    fileName = appData + fileName;
    window.$ele.fs.appendFile( fileName, logName + " " + dump + '\n', (args) => {
     
    });
   
  } catch (error) {
    console.log({ error });
  }
  
};
export const dumpLog = (...args) => {
  if (storage.get('log')){
    dumpLogError(args);
  } else {    
    console.log(args);
  }
   
};

export const tipError = (err) => {
  console.error(err, err.code);
  dumpLogError(err);
  
  if (err.code && fsErrorCode[err.code]){ 
    err = createError(err.code, err);
  }
  if (!err.info) return; //// 如果没有info则说明不是捕获过的异常，不提示
 
  err = createError(errorCode.DEFAULT_ERROR, err);
  const status = err.info.code;
  const msg = errorConfig[status] || fsErrorCode[status];
  if ([5006, 5007, 5003, 5002, 5004, 4000, 5008, 150, 4001, 4002, 4005, 4015, 400].includes(status)){
    window.app.handleRecordsave();
  }
  if (!msg) return;
  window.app.$messageBox.error({
    id: status,
    text: msg,
    duration: 3000,
  });
};

Vue.config.errorHandler = function (err, vm, info) {
  tipError(err);

};

window.onerror = (message, source, lineno, colno, err ) => {
  tipError(err);
};
window.addEventListener('unhandledrejection', function (err) {
  err.reason && tipError(err.reason);

});
